/**
 *******************************************************************************
 * @FileName  : hal_flash.c
 * @Author    : GaoQiu
 * @CreateDate: 2023-08-01
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *******************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */
#include "platform/platform.h"

int HalFlashEraseSector(uint32_t addr)
{
	FLASH_EraseSector(addr);
	return 0;
}

int HalFlashWrite(uint32_t addr, uint8_t *pdata, uint32_t len)
{
	uint32_t dataLen = FLASH_PAGE_SIZE - (addr % FLASH_PAGE_SIZE);

	dataLen = min(len, dataLen);

	if(dataLen){
		FLASH_WritePage(addr, pdata, dataLen);
		pdata += dataLen;
		len   -= dataLen;
		addr  += dataLen;
	}

	while(len)
	{
		dataLen = (len >= FLASH_PAGE_SIZE) ? FLASH_PAGE_SIZE : len;
		FLASH_WritePage(addr, pdata, dataLen);

		addr  += dataLen;
		pdata += dataLen;
		len   -= dataLen;
	}

	return 0;
}

int HalFlashRead(uint32_t addr, uint8_t *pRes, uint32_t len)
{
	FLASH_ReadPage(addr, pRes, len);
	return 0;
}




